package com.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.helper.DBHelper;
import com.model.Exam;

public class ExamDao {

	private Exam toExam(Map<String, Object> data) 
	{
		Exam e = new Exam();
		e.setId((Integer)data.get("id"));
		e.setName((String)data.get("name"));
		e.setEnable((Boolean)data.get("enable"));
		e.setClassId((String)data.get("class_id"));
		e.setPaperId((Integer)data.get("paper_id"));
		return e;
	}
	
	public List<Exam> getAll() 
	{
		List<Exam> result = new ArrayList<>();
		String sql = "select * from exam";
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result.add(toExam(ele));
		}
		return result;
	}

	public boolean add(String examName) {
		String sql = "INSERT INTO `exam` (`name`) VALUES ('"  + examName + "');";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public boolean delete(String examName) {
		String sql = "DELETE FROM `exam` WHERE `name`='"+ examName +"'";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public Exam getById(String id) {
		Exam result = new Exam();
		String sql = "select * from exam where id=" + id;
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result = toExam(ele);
		}
		return result;
	}

	public int getTotalQuestionsNum(int eid) {
		PaperDao paperDao = new PaperDao();
		return paperDao.getQuestionNum(getById(eid+"").getPaperId());
	}
	
	public Exam getByName(String Name) {
		Exam result = new Exam();
		String sql = "select * from exam where name='" + Name + "'";
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result = toExam(ele);
		}
		return result;
	}

	public List<Exam> getByPaperId(int pid) {
		List<Exam> result = new ArrayList<>();
		String sql = "select * from exam where paper_id=" + pid;
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result.add(toExam(ele));
		}
		return result;
	}

	public boolean enable(int eid) {
		String sql = "UPDATE `exam` SET `enable`= 1 WHERE (`id`="+ eid +")";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public boolean disable(int eid) {
		String sql = "UPDATE `exam` SET `enable`= 0 WHERE (`id`="+ eid +")";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public boolean updateClassId(String classId, int eid) {
		String sql = "UPDATE `exam` SET `class_id`= " + "'" + classId + "'" + " WHERE (`id`="+ eid +")";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}
	
	public boolean updatePaperId(String paperId, int eid) {
		String sql = "UPDATE `exam` SET `paper_id`= " + "'" + paperId + "'" + " WHERE (`id`="+ eid +")";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public List<Exam> getByClassId(String classId) {
		List<Exam> all = getAll();
		List<Exam> result = new ArrayList<>();
		for(int i=0; i<all.size(); ++i) {
			if(all.get(i).getClassId().contains(";"+classId+";")) {
				result.add(all.get(i));
			}
		}
		return result;
	}
}
